//
//  RankDetailViewController.m
//  海角音乐
//
//  Created by dllo on 15/8/17.
//  Copyright (c) 2015年 杨元石. All rights reserved.
//

#import "RankDetailViewController.h"

@interface RankDetailViewController ()

@end

@implementation RankDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    
    CGFloat title_width = SCREEN_WIDTH*0.533333; // 200
    CGFloat title_height = SCREEN_WIDTH*0.08; // 30
    
    self.TitleView = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, title_width, title_height)];
    self.TitleView.textColor = [UIColor whiteColor];
    self.TitleView.textAlignment = NSTextAlignmentCenter;
    self.TitleView.text = self.rankModel.title;
    self.navigationItem.titleView = self.TitleView;
    
    url_arr = [[NSMutableArray alloc] init];
    [self setAutomaticallyAdjustsScrollViewInsets:NO];
    _reModelArr = [[NSMutableArray alloc] init];
    
    _normalView = [[NormalView alloc] initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT)];
    _normalView.tableView.dataSource = self;
    _normalView.tableView.delegate = self;
    _normalView.tableView.separatorColor = [UIColor clearColor];
    _normalView.tableView.backgroundColor = [UIColor clearColor];
    [_normalView getURLForImage:self.rankModel.pic_url textForLabel:self.rankModel.desc];
    
    [self.view addSubview:_normalView];
    [_normalView release];
    
    
    HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    HUD.removeFromSuperViewOnHide = YES;
    HUD.labelText = @"loading...";
    [self.view addSubview:HUD];
    HUD.mode = MBProgressHUDModeIndeterminate;
    [self getDataForSingerName];

    
    BarPlayer = [DanLiPlayer shareHandleDanLiPlayer:CGRectZero];
    BarPlayer.frame = CGRectMake(0, SCREEN_HEIGHT-50, SCREEN_WIDTH, 50);
    [self.view addSubview:BarPlayer];

    

}


- (void)getDataForSingerName
{
    NSString *url_one = @"http://api.dongting.com/channel/ranklist/";
    NSString *page_three = @"/songs?page=1&app=ttpod&v=v7.9.4.2015052918&uid=&mid=iPhone5C&f=f320&s=s310&imsi=&hid=&splus=8.3&active=1&net=2&openudid=8ab7f0f87beab7e9bd24db8751cdfb44a3f8ba8f&idfa=EE9C3C6E-4AF6-4C81-B2C0-1A80104187A0&utdid=Vb8Xammx8HcDAJ6K4SjYk1ID&alf=201200&bundle_id=com.ttpod.music";
    
    NSString *url_string = [NSString stringWithFormat:@"%@%@%@", url_one, self.rankModel.rankId, page_three];    
    
    AFNetworkReachabilityManager *netWorkManager = [AFNetworkReachabilityManager sharedManager];
    
    AFHTTPRequestOperationManager* manager = [AFHTTPRequestOperationManager manager];
    
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html", @"application/x-javascript",nil];
    
    [manager GET:url_string parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        [netWorkManager stopMonitoring];
        
       
        
        NSArray *dataArr = [responseObject objectForKey:@"data"];
        
        for (NSDictionary *dic in dataArr) {
            
            ReDetailModel *reDetailkModel = [[ReDetailModel alloc] init];
            
            [reDetailkModel setValuesForKeysWithDictionary:dic];
         
            [_reModelArr addObject:reDetailkModel];
            
        }
        
        [HUD setHidden:YES];
        [_normalView.tableView reloadData];
        _normalView.tableView.separatorColor = [UIColor lightGrayColor];
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        

        
        NSLog(@"失败==== %@",error);
        
    }];
    
    
    
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    return _reModelArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellId = @"cell";
    
    ReDetailTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    
    if (cell == NO) {
        
        cell = [[ReDetailTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
    }
    
    if (_reModelArr.count > indexPath.row) {
      
        ReDetailModel *redeModel = [_reModelArr objectAtIndex:indexPath.row];
        
        [cell getDataForCell:redeModel];
        
        cell.textLabel.text = [NSString stringWithFormat:@"%ld", indexPath.row+1];
    }
    
    
    return cell;
    
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    return SCREEN_WIDTH*0.213333;  // 80
    
}

#pragma mark --  播放的逻辑
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    for (ReDetailModel *reDetailModel in _reModelArr) {
        
        if (reDetailModel.auditionList.count > 1) {
            
            NSDictionary *dic = [reDetailModel.auditionList objectAtIndex:1];
            NSString *url_string = [dic objectForKey:@"url"];
            
            [url_arr addObject:url_string];
            
        } 
        
    }
    
    ReDetailModel *reDetailmodel = [_reModelArr objectAtIndex:indexPath.row];
    BarPlayer.reDetailModel = reDetailmodel;
    BarPlayer.dataArr = _reModelArr;
    BarPlayer.index = indexPath.row;

    [self playeMusicWithModel:url_arr indexPath:indexPath];
    
    
    
}
// 播放方法
- (void)playeMusicWithModel:(NSMutableArray *)url_Arr indexPath:(NSIndexPath *)indexpath;
{
    
    
    NSString *url_string = [url_arr objectAtIndex:indexpath.row];
    
    STKAudioPlayer *Legacy = [SinglePlayer shareHandleSinglePlayer];
    
    [Legacy play:url_string];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
